const Service = require("egg").Service;

class AgentService extends Service {

    async getList() {
        const { ctx, app } = this;
        let {
            pageIndex,
            pageSize,
            tel,
            state
        } = ctx.request.body;
        let sql = "select * from userAgents where id=id "
        if (tel) {
            sql += `and tel like "%${tel}%" `
        }
        if (state !== undefined) {
            sql += `and state like "%${state}%" `
        }
        let res = await app.mysql.query(sql);
        let data = res.slice((pageIndex - 1) * pageSize, pageIndex * pageSize);
        return {
            total: res.length,
            data
        }
    }
    //删除
    async del() {
        const { ctx, app } = this;
        const { id } = JSON.parse(ctx.query.params);
        let res = await app.mysql.delete("userAgents", { id });
        return res;
    }
    //编辑
    async edit() {
        const { ctx, app } = this;
        let {
            nickname,
            tel,
            state,
            company,
            id,
            Enable
        } = ctx.request.body;
        let res;
        if (Enable !== undefined) {
            res = await app.mysql.update("userAgents", {
                id,
                Enable
            })
        } else {
            res = await app.mysql.update("userAgents", {
                nickname,
                tel,
                state,
                company,
                id
            })
        }

        return res
    }
    //添加
    async add() {
        const { ctx, app } = this;
        let {
            nickname,
            tel,
            state,
            company,
            username,
            password
        } = ctx.request.body;
        let res = await app.mysql.insert("userAgents", {
            nickname,
            tel,
            state,
            company,
            username,
            password
        })
        return res;
    }
}
module.exports = AgentService;